<?php
namespace app\branch\controller;
use think\Controller;
use think\Session;
use think\Request;
/**
 * 分公司营地管理
 */
class Campcontrol extends Controller
{   
    use \app\user\traits\models\UserInfoCheck;
    use \app\common\traits\controller\TraitController;
    use \app\common\traits\controller\TraitUpload;
    //登陆与权限判断
     protected $beforeActionList = [
        'first',
        'checkLogin',
        
    ];
    protected function checkLogin()
    {
        if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
    }
    //地区插件
    public function getCountry()
    {
        $country = db('area')->where('type',0)->field(['area_code','parent_code','area_name','type'])->select();;
        // echo   json_encode($country);
        // exit;
        return json($country);
    }
    public function getProvince()
    {
        $addressProvince = input('addressCountry');
        $province = db('area')->where('type',1)->where('parent_code',$addressProvince)->field(['area_code','parent_code','area_name','type'])->select();
        return  json($province);
    }
    public function getCity()
    {
        $city = input('addressProvince');
        $city = db('area')->where('type',2)->where('parent_code',$city)->field(['area_code','parent_code','area_name','type'])->select();
        return json($city);
    }
    /**
     * [getList 营地管理列表]
     * @return [type] [description]
     */
    public function getindex()
    {
        //取本公司下的营地
        $lo_bran_id = model('Managers')->getLoginUserInfo('branch_id');
        $campinfos = model('Campcontrols')->where('branch_id',$lo_bran_id)->select();
        $this->assign('campinfos',$campinfos);
        //搜索条件
        $seek = model('Campcontrols')->getSeekName();
        $this->assign('seek',$seek);
        //取本公司下的营地
        $list = model('Campcontrols')->getCampList(array_merge(input('get.'),['branch_id'=>$lo_bran_id]));//halt($list);
        return view('campcontrol/index',['list'=>$list]);
    }
    /**
     * [create 添加营地]
     * @return [type] [description]
     */
    public function getCreate()
    {
        $bt = $this->old();
        if (empty($bt)) {
            $bt['address'] = '';
            $bt['name'] = '';
            $bt['count'] = '';
            $bt['campmanager_phone'] = '';
            $bt['campmanager_name'] = '';
            $bt['tel'] = '';
            $bt['legal_person'] = '';
        }
        $types = db('camp_types')->column('name', 'id');//类型
        $grades = db('camp_grades')->column('name', 'id');//星级

        return view('campcontrol/create',['types'=>$types,'grades'=>$grades,'bt'=>$bt]);
    }

    public function postSave()
    {
        $this->flash();
        $post_camp_info['name'] = input('post.name');
        $post_camp_info['number'] = model('Campcontrols')->quickRandom(12);
        $post_camp_info['type'] = input('post.type');
        $post_camp_info['grade'] = input('post.grade');
        $post_camp_info['count'] = input('post.count');
        $post_camp_info['lat'] = input('post.lat/f');
        $post_camp_info['lng'] = input('post.lng/f');
        $post_camp_info['profiles'] = input('post.profiles');
        $post_camp_info['content'] = input('post.content');
        $post_camp_info['email'] = input('post.email');
        //由总公司添加基数
        //$post_camp_info['travel_money_base'] = input('post.travel_money_base');
        $post_camp_info['legal_person'] = input('post.legal_person');
        $post_camp_info['credit_code'] = input('post.credit_code');
        $branch_id = model('Managers')->getLoginUserInfo('branch_id');
        $post_camp_info['branch_id'] = $branch_id;
        $post_camp_info['branch_name'] = db('branch_info')->where('id',$branch_id)->value('name');
        $post_camp_info['campmanager_phone'] = input('post.campmanager_phone');
        $post_camp_info['campmanager_name'] = input('post.campmanager_name');
        $post_camp_info['cover'] = input('post.cover/a');//营地封面
        $post_camp_info['license'] = input('post.license/a');//营业执照       
        $post_camp_info['tel'] = input('post.tel');
        $post_camp_info['address'] = input('post.address');
        //国家-省份-城市
        $post_camp_info['country'] = input('post.addressCountry');
        $post_camp_info['province'] = input('post.addressProvince');
        $post_camp_info['city'] = input('post.addressCity');
        $post_camp_info['status'] = -99;//未审核该营地
        //dump($post_camp_info);die;
        //检测必填项有问题
        //$bt['country'] = $post_camp_info['country'];
        $bt['email'] =  $post_camp_info['email'];//必填邮箱
        $bt['province'] = $post_camp_info['province'];
        $bt['city'] =  $post_camp_info['city'];
        $bt['address'] = $post_camp_info['address'];
        $bt['name'] = $post_camp_info['name'];
        $bt['count'] = $post_camp_info['count'];
        $bt['campmanager_phone'] = $post_camp_info['campmanager_phone'];
        $bt['campmanager_name'] = $post_camp_info['campmanager_name'];
        $bt['tel'] = $post_camp_info['tel'];
        $bt['legal_person'] = $post_camp_info['legal_person'];
        //halt($bt);
        $re = $this->getCheckNull($bt);
        if (!$re) {
            return $this->error('请完善必填项',$url = null,'',$wait=3);
        }
        //邮件判断
        $re1 = $this->cheack(['email'=>$post_camp_info['email']]);
        if (!$re1) {
           return $this->error('邮件格式不正确',$url = null,'',$wait=3); 
        }
        $camp = model('Campcontrols')->addCampInfo($post_camp_info);
        if($camp){
            return $this->success(lang('营地添加成功'), '/campcontrol/index');
        }
        return $this->error(lang('营地添加失败'));
    }
    /**
     * [getCheckEmail 检测邮箱格式]
     * @return [type] [description]
     */
    public function getCheckEmail()
    {
        $email = input('email');
        $re = $this->cheack(['email'=>$email]);
        if ($re) {
            return 1;
        }else{
            return json(0);
        }
    }
    /**
     * [getCheckNickName 检测空字段]
     * @return [type] []
     */
    public function getCheckNull($info_array)
    {
        foreach ($info_array as $key => $value) {
            
            if ($value=='' && empty($value) && $value==0 && $value==null) {

                return false;
            }
        }
        return true;
    }
    /**
     * [postUpload 图片上传]
     * @return [type] [description]
     */
    public function postUploadImg()
    {

        if ($this->upload('file')) {
            $url = $this->getUploadInfos();
            return json(array(
                "state" => 'SUCCESS',
                "url" => $url['url'],
            ));
        }
    }
    /**
     * [postUpload 百度编辑器图片上传]
     * @return [type] [description]
     */
    public function postUploadImage()
    {

        if ($this->upload('upfile')) {
            $url = $this->getUploadInfos();
            return json(array(
                "state" => 'SUCCESS',
                "url" => $url['url'],
            ));
        }
    }
    /**
     * [update 编辑营地信息]
     * @return [type] [description]
     */
    public function getEditInfo($id)
    {
        //判断误操作
        //$this->checkAbility($camp_id);
        $list = model('Campcontrols')->getCampIofo($id);
        $types = db('camp_types')->column('name', 'id');//类型
        $grades = db('camp_grades')->column('name', 'id');//星级
        return view('campcontrol/edit',['types'=>$types,'grades'=>$grades,'list'=>$list]);
    }

    public function postUpdateInfo()
    {
        //地区名称限制不能使用
        //$info = input('');$id = input('post.id');
        //$info['address'] = [input('post.country'),input('post.state'),input('post.city')];
        //halt($info);
        $post_camp_info['name'] = input('post.name');
        $post_camp_info['type'] = input('post.type');
        $post_camp_info['grade'] = input('post.grade');
        $post_camp_info['count'] = input('post.count');
        $post_camp_info['lat'] = input('post.lat/f');
        $post_camp_info['lng'] = input('post.lng/f');
        $post_camp_info['profiles'] = input('post.profiles');
        $post_camp_info['content'] = input('post.content');
        $post_camp_info['email'] = input('post.email');
        //由总公司编辑基数
        //$post_camp_info['travel_money_base'] = input('post.travel_money_base');
        $post_camp_info['legal_person'] = input('post.legal_person');
        $post_camp_info['credit_code'] = input('post.credit_code');
        //$branch_name = model('Managers')->getLoginUserInfo('branch_name');
        //$post_camp_info['branch_name'] = $branch_name;
        $post_camp_info['campmanager_phone'] = input('post.campmanager_phone');
        $post_camp_info['campmanager_name'] = input('post.campmanager_name');
        $a = input('post.cover/a');$b = input('post.license/a');
        if (!empty($a)) {
            $post_camp_info['cover'] = $a;//营地封面
        }
        if (!empty($b)) {
            $post_camp_info['license'] = $b;//营业执照
        }
        //halt($post_camp_info['license']);
        $post_camp_info['tel'] = input('post.tel');
        $post_camp_info['address'] = input('post.address');
        //国家-省份-城市
        $post_camp_info['country'] = input('post.addressCountry');
        $post_camp_info['province'] = input('post.addressProvince');
        $post_camp_info['city'] = input('post.addressCity');
        $id = input('post.id');
        //检测必填项
        //$bt['country'] = $post_camp_info['country'];
        $bt['email'] =  $post_camp_info['email'];//必填邮箱
        $bt['province'] = $post_camp_info['province'];
        $bt['city'] =  $post_camp_info['city'];
        $bt['address'] = $post_camp_info['address'];
        $bt['name'] = $post_camp_info['name'];
        $bt['count'] = $post_camp_info['count'];
        $bt['campmanager_phone'] = $post_camp_info['campmanager_phone'];
        $bt['campmanager_name'] = $post_camp_info['campmanager_name'];
        $bt['tel'] = $post_camp_info['tel'];
        $bt['legal_person'] = $post_camp_info['legal_person'];
        $re = $this->getCheckNull($bt);
        if (!$re) {
            return $this->error('请完善必填项',$url = null,'',$wait=3);
        }
        //邮件判断
        $re1 = $this->cheack(['email'=>$post_camp_info['email']]);
        if (!$re1) {
           return $this->error('邮件格式不正确',$url = null,'',$wait=3); 
        }
        //halt($post_camp_info);
        $result = model('Campcontrols')->updateCampInfo($id,$post_camp_info);
        if($result){
            return $this->success(lang('更新成功'), '/campcontrol/index');
        }
        return $this->error(lang('更新失败'));
    }
    /**
     * [getReadInfo 查看营地信息]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getReadInfo($id)
    {
        //判断误操作
        //$this->checkAbility($camp_id);
        $list =model('Campcontrols')->getCampIofo($id);
        return view('campcontrol/read',['list'=>$list]);
    }

    public function getAddCampManager($id)
    {
        $list = db('camp_manager')->where('camp_id',$id)->find();
        //halt($list);
        return view('campcontrol/campmanagerinfo',['list'=>$list]);
    }
    /**
     * [FunctionName 添加营地管理员]
     * @param string $value [description]
     */
    public function postUpdateCampManagerInfo()
    {
        $id = input('post.id');//halt($id);
        $phone = input('post.phone');
        $name = input('post.name');
        $email = input('post.email');
        $password = md5(input('post.phone'));
        $remark = input('post.remark');
        $status = 0;//超管状态
        //判断误操作
        $lo_bran_id = model('Managers')->getLoginUserInfo('branch_id');
        //判断该营地是否有管理员
        $r = db('camp_manager')->where('camp_id',$id)->find();
        if ($r) {
            //更新
            $re = db('camp_manager')->where('camp_id',$id)->update(['phone'=>$phone,'name'=>$name,'email'=>$email,'password'=>$password,'remark'=>$remark,'status'=>$status]);
            if ($re) {
                return $this->success(lang('update success'),"/campcontrol/$id/addcampmanager");
            }else{
                return $this->error(lang('update failed'));
            }
        }else{
            //插入
            $date = ['phone'=>$phone,'name'=>$name,'email'=>$email,'camp_id'=>$id,'password'=>$password,'remark'=>$remark,'branch_id'=>$lo_bran_id,'status'=>$status];
            $re = db('camp_manager')->insert($date);
            if ($re) {
                return $this->success(lang('添加成功'),"campcontrol/$id/addcampmanager");
            }else{
                return $this->error(lang('添加失败'));
            }
        }
    }
    //判断权限
    public function checkAbility($camp_id)
    {
        //取登陆者分公司id
        $lo_bran_id = model('Managers')->getLoginUserInfo('branch_id');
        //取营地分公司id
        $cap_bran_id = model('Campcontrol')->where('branch_id',$camp_id)->value('branch_id');
        if ($lo_bran_id<>$cap_bran_id) {
           return $this->error(lang(' wrong operation'));
        }
    }

}
